package com.woniuxy.movie.user.controller;


import com.woniuxy.movie.commons.core.model.Result;
import com.woniuxy.movie.commons.core.util.JwtUtil;
import com.woniuxy.movie.user.dto.AccountDto;
import com.woniuxy.movie.user.form.PassLoginForm;
import com.woniuxy.movie.user.form.RegisterAccountForm;
import com.woniuxy.movie.user.service.UserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XIAO
 * @since 2023年10月10日
 */
@RestController
@RequestMapping("/userAccount")
@Api(tags = "用户服务")
public class UserAccountController {

    @Autowired
    UserAccountService userAccountService;

    @PostMapping("/register")
    @ApiOperation("账号注册")
    public Result<AccountDto> register(@Valid @RequestBody RegisterAccountForm form) throws Exception {

        AccountDto accountDto = userAccountService.register(form);

        return Result.getResult(AccountDto.class).setData(accountDto).setMsg("注册成功");
    }

    @PostMapping("/passLogin")
    @ApiOperation("用户名或手机号登录")
    public Result<AccountDto> passLogin(@Valid @RequestBody PassLoginForm form, HttpServletResponse response){

        AccountDto accountDto = userAccountService.passLogin(form);
        HashMap<String, Object> map = new HashMap<>();
        map.put("accountId",accountDto.getAccountId());
        String authen_token = JwtUtil.createUserToken(map, 30);
        response.setHeader("authen_token",authen_token);

        return Result.getResult(AccountDto.class).setData(accountDto).setMsg("登录成功");
    }

}

